package com.sykj.huxianlong.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.servlet.ServletUtil;

import com.sykj.huxianlong.entity.SysConstant;
import com.sykj.huxianlong.entity.User;
import com.sykj.huxianlong.service.ICacheService;
import com.sykj.huxianlong.service.IUserService;
import com.sykj.util.reponse.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: UserInfoController
 * @Description:
 * @Author: sunt
 * @Date: 2020/6/3 10:07
 * @Version 1.0
 **/
@RequestMapping("user")
@RestController
@CrossOrigin
@Slf4j
public class UserInfoController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ICacheService cacheService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 登录
     * @param user
     * @param response
     * @return
     */
    @PostMapping("/login")
    public ResultBody doLogin(@RequestBody User user, HttpServletResponse response) {
       // ResponseResult<User> userResponseResult = userService.doLogin(user.getUserName(), user.getUserPass());
        User users= userService.doLogin(user.getUserName(), user.getUserPass());
        String userkey="user";
        redisTemplate.opsForValue().set(userkey, users);
        String token = IdUtil.fastSimpleUUID();
        System.out.println(response);
      //  ServletUtil.addCookie(response, SysConstant.ACTIVITI_COOKIE, token,-1); //关闭浏览器登录失效
        cacheService.cacheObjData(token,users, 60);
       return    new ResultBody("11","登录成功");


    }

}
